package com.creditease.mobile.rss.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.creditease.mobile.rss.RssConst;
import com.creditease.mobile.rss.model.RssModel;

public class RssDB extends SQLiteOpenHelper {

	private final static String DB_NAME = "RSS";
	private final static int version = 1;

	public RssDB(Context context) {
		super(context, DB_NAME, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE rss (id integer primary key autoincrement, title varchar(128),url varchar(128),desc varchar(128),logoUrl varchar(128),pubDate varchar(128),enCode varchar(32),isRead,isSub)";
		db.execSQL(sql);
		String insertSql0 = "insert into rss (id,title,url,desc,logoUrl,pubDate,enCode,isRead,isSub) values (?,?,?,?,?,?,?,?,?)";
		
		db.execSQL(insertSql0, new Object[] { RssConst.FAV_ID,"我的收藏",
		                     				"", "",
		                     				"", new Date().getTime()+"",
		                     				"UTF-8" ,"0","1"});
		
		String insertSql = "insert into rss (title,url,desc,logoUrl,pubDate,enCode,isRead,isSub) values (?,?,?,?,?,?,?,?)";
		
		db.execSQL(insertSql, new Object[] { "有意思吧",
				"http://www.u148.net/rss/", "分享·情怀·超越 ，不仅仅是娱乐！",
				"http://www.u148.net/images/logo.gif", new Date().getTime()+"",
				"UTF-8" ,"0","1"});
		db.execSQL(insertSql,
				new Object[] { "糗事", "http://www.qiushi.com/rss.html", "很冷",
						"", new Date().getTime()+"", "UTF-8" ,"0","1"});
		db.execSQL(insertSql, new Object[] { "新闻要闻",
				"http://rss.sina.com.cn/news/marquee/ddt.xml", "新闻要闻-新浪新闻",
				"http://www.sinaimg.cn/home/deco/2009/0330/logo_home_news.gif",
				new Date().getTime()+"", "UTF-8","0","0" });
		db.execSQL(insertSql, new Object[] { "生活博客",
				"http://rss.sina.com.cn/blog/index/enjoy.xml", "新浪生活博客",
				"http://www.sinaimg.cn/blog/main/index2010/blog_logo.png",
				new Date().getTime()+"", "UTF-8" ,"0","1"});
		db.execSQL(insertSql, new Object[] { "观点博客",
				"http://rss.sina.com.cn/blog/index/other.xml", "新浪观点博客",
				"http://www.sinaimg.cn/blog/main/index2010/blog_logo.png",
				new Date().getTime()+"", "UTF-8" ,"0","1"});
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	public long save(RssModel model) {
		SQLiteDatabase db = getWritableDatabase();
		/*
		 * String sql =
		 * "insert into rss (title,url,desc,logoUrl,pubDate,enCode) values (?,?,?,?,?,?)"
		 * ; db.execSQL(sql, new Object[]{ model.title, model.url, model.desc,
		 * model.logoUrl, model.pubDate, model.enCode});
		 */
		ContentValues cv = new ContentValues();
		cv.put("title", model.title);
		cv.put("url", model.url);
		cv.put("desc", model.desc);
		cv.put("logoUrl", model.logoUrl);
		cv.put("pubDate", model.pubDate);
		cv.put("enCode", model.enCode);
		cv.put("isRead", false);
        cv.put("isSub",model.isSub);
		return db.insert("rss", null, cv);
	}

	public void delete(Object id) {
		SQLiteDatabase db = getWritableDatabase();
		String sql = "delete from rss  where id=?";
		db.execSQL(sql, new Object[] { id });
	}

	public List<RssModel> findAll(boolean all) {
		SQLiteDatabase db = getWritableDatabase();
		List<RssModel> result = new ArrayList<RssModel>();
		String sql = "select * from rss order by id";
		Cursor cursor = null;
		try {
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				int id = cursor.getInt(0); // 获取第一列的值,第一列的索引从0开始
				String title = cursor.getString(1);// 获取第二列的值
				String url = cursor.getString(2);// 获取第二列的值
				String link = cursor.getString(3);// 获取第二列的值
				String logoUrl = cursor.getString(4);// 获取第二列的值
				String pubDate = cursor.getString(5);// 获取第二列的值
				RssModel model = new RssModel(title, url, link, logoUrl,
						pubDate);
				model.id = String.valueOf(id);
				model.enCode = cursor.getString(6);
				boolean isRead = cursor.getInt(7) == 1 ? true : false;
                boolean isSub = cursor.getInt(8) == 1 ? true : false;
				model.isRead = isRead;
                model.isSub=isSub;
				if(all|| isSub){
                    result.add(model);
                }
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			db.close();
		}
		return result;
	}

	/**
	 * Description:
	 * 
	 * @param
	 * @return RssModel
	 * @throws
	 * @Author liong Create Date: 2012-3-19 下午1:29:22
	 */

	public synchronized RssModel findById(String id) {
		Log.i("A", "id=" + id);
		String sql = "select * from rss where id=?";
		Cursor cursor = null;
		SQLiteDatabase db = getWritableDatabase();
		try {
			cursor = db.rawQuery(sql, new String[] { id });
			cursor.moveToNext();
			int ids = cursor.getInt(0); // 获取第一列的值,第一列的索引从0开始
			String title = cursor.getString(1);// 获取第二列的值
			String url = cursor.getString(2);// 获取第二列的值
			String link = cursor.getString(3);// 获取第二列的值
			String logoUrl = cursor.getString(4);// 获取第二列的值
			String pubDate = cursor.getString(5);// 获取第二列的值
			RssModel model = new RssModel(title, url, link, logoUrl, pubDate);
			model.enCode = cursor.getString(6);
			model.id=id;
			boolean isRead = cursor.getInt(7) == 1 ? true : false;
			model.isRead = isRead;
			return model;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			db.close();
		}

	}
	
	/**
	 * Description:
	 * 
	 * @param
	 * @return RssModel
	 * @throws
	 * @Author liong Create Date: 2012-3-19 下午1:29:22
	 */

	public synchronized void updateById(RssModel model) {
		
		Cursor cursor = null;
		SQLiteDatabase db = getWritableDatabase();
		try {
			ContentValues cv=new ContentValues();
			cv.put("desc", model.desc);
			cv.put("pubDate", model.pubDate);
			cv.put("isRead", model.isRead);
            cv.put("isSub", model.isSub);
			db.update("rss", cv, "id=?", new String[]{model.id});		
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			db.close();
		}

	}
}
